<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>游戏房间 - 网络五子棋对战游戏</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Microsoft YaHei', sans-serif;
        }

        body {
            background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
            height: 100vh;
            display: flex;
            flex-direction: column;
            color: white;
        }

        .nav {
            width: 100%;
            padding: 20px;
            text-align: center;
            font-size: 1.8rem;
            font-weight: bold;
            background: rgba(0, 0, 0, 0.2);
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .container {
            flex: 1;
            display: flex;
            padding: 20px;
            gap: 30px;
        }

        #chess_area {
            flex: 1;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
        }

        #chess {
            background: #e9c46a;
            border-radius: 5px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
        }

        #screen {
            margin-top: 20px;
            padding: 15px 30px;
            background: rgba(0, 0, 0, 0.3);
            border-radius: 10px;
            font-size: 1.2rem;
            text-align: center;
            min-width: 200px;
        }

        .my-turn {
            background: rgba(76, 175, 80, 0.3);
            animation: glow 1.5s infinite alternate;
        }

        @keyframes glow {
            from {
                box-shadow: 0 0 5px rgba(76, 175, 80, 0.5);
            }

            to {
                box-shadow: 0 0 15px rgba(76, 175, 80, 0.8);
            }
        }

        #chat_area {
            width: 300px;
            display: flex;
            flex-direction: column;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            border-radius: 15px;
            overflow: hidden;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }

        #chat_show {
            flex: 1;
            padding: 15px;
            overflow-y: auto;
            max-height: 400px;
        }

        #msg_show {
            padding: 15px;
            border-top: 1px solid rgba(255, 255, 255, 0.2);
            display: flex;
            gap: 10px;
        }

        #chat_input {
            flex: 1;
            padding: 10px;
            border: none;
            border-radius: 5px;
            background: rgba(255, 255, 255, 0.9);
        }

        #chat_button {
            padding: 10px 15px;
            background: linear-gradient(45deg, #ff8a00, #e52e71);
            border: none;
            border-radius: 5px;
            color: white;
            cursor: pointer;
        }

        #self_msg {
            text-align: right;
            color: #a8e6cf;
            margin-bottom: 10px;
        }

        #peer_msg {
            text-align: left;
            color: #ffd3b6;
            margin-bottom: 10px;
        }

        .game-over {
            text-align: center;
            margin-top: 20px;
        }

        .back-button {
            padding: 10px 20px;
            background: rgba(255, 255, 255, 0.2);
            border: 1px solid rgba(255, 255, 255, 0.3);
            color: white;
            border-radius: 5px;
            cursor: pointer;
            transition: all 0.3s;
            margin-top: 15px;
        }

        .back-button:hover {
            background: rgba(255, 255, 255, 0.3);
        }

        .player-info {
            display: flex;
            justify-content: space-between;
            width: 450px;
            margin-bottom: 15px;
        }

        .player {
            padding: 10px 20px;
            background: rgba(0, 0, 0, 0.3);
            border-radius: 10px;
            text-align: center;
            flex: 1;
            margin: 0 10px;
        }

        .current-player {
            background: rgba(76, 175, 80, 0.3);
            animation: glow 1.5s infinite alternate;
        }
    </style>
</head>

<body>
    <div class="nav">网络五子棋对战游戏</div>
    <div class="container">
        <div id="chess_area">
            <div class="player-info">
                <div class="player" id="white-player">白棋: 等待中...</div>
                <div class="player" id="black-player">黑棋: 等待中...</div>
            </div>
            <canvas id="chess" width="450px" height="450px"></canvas>
            <div id="screen">等待玩家连接中...</div>
        </div>
        <div id="chat_area">
            <div id="chat_show">
                <p>系统: 欢迎来到五子棋对战房间！</p><br>
            </div>
            <div id="msg_show">
                <input type="text" id="chat_input" placeholder="输入聊天内容...">
                <button id="chat_button">发送</button>
            </div>
        </div>
    </div>

    <script>
        let chessBoard = [];
        let BOARD_ROW_AND_COL = 15;
        let chess = document.getElementById('chess');
        let context = chess.getContext('2d');

        var ws_url = "ws://" + location.host + "/room";
        var ws_hdl = new WebSocket(ws_url);

        var room_info = null;
        var is_me;

        function initGame() {
            initBoard();
            context.strokeStyle = "#8B4513";
            context.lineWidth = 1.5;

            let logo = new Image();
            logo.src = "image/shangxinci.jpeg";
            logo.onload = function () {
                context.drawImage(logo, 0, 0, 450, 450);
                drawChessBoard();

                // 绘制棋盘上的五个点
                drawPoints();
            }
        }

        function drawPoints() {
            context.fillStyle = "#8B4513";
            // 天元和四个星位
            let points = [
                [7, 7], [3, 3], [11, 3],
                [3, 11], [11, 11]
            ];

            points.forEach(point => {
                context.beginPath();
                context.arc(15 + point[0] * 30, 15 + point[1] * 30, 4, 0, 2 * Math.PI);
                context.fill();
            });
        }

        function initBoard() {
            for (let i = 0; i < BOARD_ROW_AND_COL; i++) {
                chessBoard[i] = [];
                for (let j = 0; j < BOARD_ROW_AND_COL; j++) {
                    chessBoard[i][j] = 0;
                }
            }
        }

        function drawChessBoard() {
            for (let i = 0; i < BOARD_ROW_AND_COL; i++) {
                context.beginPath();
                context.moveTo(15 + i * 30, 15);
                context.lineTo(15 + i * 30, 435);
                context.stroke();

                context.beginPath();
                context.moveTo(15, 15 + i * 30);
                context.lineTo(435, 15 + i * 30);
                context.stroke();
            }
        }

        function oneStep(i, j, isWhite) {
            if (i < 0 || j < 0) return;
            context.beginPath();
            context.arc(15 + i * 30, 15 + j * 30, 13, 0, 2 * Math.PI);
            context.closePath();
            var gradient = context.createRadialGradient(
                15 + i * 30 + 2, 15 + j * 30 - 2, 13,
                15 + i * 30 + 2, 15 + j * 30 - 2, 0
            );

            if (!isWhite) {
                gradient.addColorStop(0, "#0A0A0A");
                gradient.addColorStop(1, "#636766");
            } else {
                gradient.addColorStop(0, "#D1D1D1");
                gradient.addColorStop(1, "#F9F9F9");
            }
            context.fillStyle = gradient;
            context.fill();
        }

        chess.onclick = function (e) {
            if (!is_me) {
                alert("等待对方走棋....");
                return;
            }

            let x = e.offsetX;
            let y = e.offsetY;
            let col = Math.floor(x / 30);
            let row = Math.floor(y / 30);

            if (chessBoard[row][col] != 0) {
                alert("当前位置已有棋子！");
                return;
            }

            send_chess(row, col);
        }

        function send_chess(r, c) {
            var chess_info = {
                op_type: "chess",
                room_id: room_info.room_id,
                uid: room_info.uid,
                row: r,
                col: c
            };
            ws_hdl.send(JSON.stringify(chess_info));
        }

        window.onbeforeunload = function () {
            ws_hdl.close();
        }

        ws_hdl.onopen = function () {
            console.log("房间长连接建立成功");
        }

        ws_hdl.onclose = function () {
            console.log("房间长连接断开");
        }

        ws_hdl.onerror = function () {
            console.log("房间长连接出错");
        }

        function set_screen(me) {
            var screen_div = document.getElementById("screen");
            if (me) {
                screen_div.innerHTML = "轮到己方走棋...";
                screen_div.classList.add("my-turn");
            } else {
                screen_div.innerHTML = "轮到对方走棋...";
                screen_div.classList.remove("my-turn");
            }

            // 更新玩家指示器
            var whitePlayer = document.getElementById("white-player");
            var blackPlayer = document.getElementById("black-player");

            if (is_me) {
                if (room_info.uid == room_info.white_id) {
                    whitePlayer.classList.add("current-player");
                    blackPlayer.classList.remove("current-player");
                } else {
                    blackPlayer.classList.add("current-player");
                    whitePlayer.classList.remove("current-player");
                }
            } else {
                if (room_info.uid == room_info.white_id) {
                    blackPlayer.classList.add("current-player");
                    whitePlayer.classList.remove("current-player");
                } else {
                    whitePlayer.classList.add("current-player");
                    blackPlayer.classList.remove("current-player");
                }
            }
        }

        ws_hdl.onmessage = function (evt) {
            var info = JSON.parse(evt.data);
            console.log(JSON.stringify(info));

            if (info.op_type == "error") {
                alert(info.result || "未知错误");
            } else if (info.op_type == "room_ready") {
                room_info = info;
                is_me = room_info.uid == room_info.white_id ? true : false;

                // 更新玩家信息显示
                document.getElementById("white-player").innerHTML = "白棋: " + room_info.white_name + "<br>积分: " + room_info.white_points;
                document.getElementById("black-player").innerHTML = "黑棋: " + room_info.black_name + "<br>积分: " + room_info.black_points;

                if (is_me) {
                    alert("你是白棋先走");
                } else {
                    alert("你是黑棋后走");
                }

                set_screen(is_me);
                initGame();
            } else if (info.op_type == "game_over") {
                var screen_div = document.getElementById("screen");
                if (room_info.uid == info.winner) {
                    screen_div.innerHTML = info.result || "恭喜！你赢了！";
                    screen_div.style.background = "rgba(76, 175, 80, 0.5)";
                } else {
                    screen_div.innerHTML = "很遗憾，你输了";
                    screen_div.style.background = "rgba(244, 67, 54, 0.5)";
                }

                screen_div.classList.remove("my-turn");

                var chess_area_div = document.getElementById("chess_area");
                var button_div = document.createElement("div");
                button_div.innerHTML = "返回大厅";
                button_div.className = "back-button";
                button_div.onclick = function () {
                    ws_hdl.close();
                    location.replace("/game_hall.html");
                }
                chess_area_div.appendChild(button_div);
            } else if (info.op_type == "chess") {
                is_me = info.uid == room_info.uid ? false : true;
                set_screen(is_me);

                isWhite = info.uid == room_info.white_id ? true : false;
                oneStep(info.col, info.row, isWhite);
                chessBoard[info.row][info.col] = 1;
            } else if (info.op_type == "chat") {
                var msg_div = document.createElement("p");
                msg_div.innerHTML = (info.uid == room_info.uid ? "你" : "对手") + ": " + info.message;

                if (info.uid == room_info.uid) {
                    msg_div.setAttribute("id", "self_msg");
                } else {
                    msg_div.setAttribute("id", "peer_msg");
                }

                var br_div = document.createElement("br");
                var msg_show_div = document.getElementById("chat_show");
                msg_show_div.appendChild(msg_div);
                msg_show_div.appendChild(br_div);

                // 自动滚动到底部
                msg_show_div.scrollTop = msg_show_div.scrollHeight;
                document.getElementById("chat_input").value = "";
            }
        }

        var cb_div = document.getElementById("chat_button");
        cb_div.onclick = function () {
            var message = document.getElementById("chat_input").value.trim();
            if (message === "") return;

            var send_msg = {
                op_type: "chat",
                room_id: room_info.room_id,
                uid: room_info.uid,
                message: message
            };
            ws_hdl.send(JSON.stringify(send_msg));
        }

        // 按回车发送消息
        document.getElementById("chat_input").addEventListener("keypress", function (event) {
            if (event.key === "Enter") {
                cb_div.click();
            }
        });
    </script>
</body>

</html>